package com.hellogood.yaohuo.client.basic;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Ziyou;
import com.hellogood.yaohuo.shared.services.ZiyouService;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class ZiyouModifyWindow extends Window {

	private TextItem codeItem = null;
	private TextItem nameItem = null;
	private Button saveBtn = null;
	private Label msg = null;
	private DynamicForm form = null;
	private ZiyouController zc = null;
	private boolean isModify;

	public ZiyouModifyWindow(ZiyouController zc, Ziyou zy, boolean isModify) {
		this.zc = zc;
		this.isModify = isModify;
		initView();
		if (isModify) {
			setTitle("修改项目");
			codeItem.setValue(zy.getZiyouCode());
			nameItem.setValue(zy.getZiyouName());
		} else {
			setTitle("创建项目");
			codeItem.setValue("*");
		}
	}

	private void initView() {
		LengthRangeValidator length10Validator = new LengthRangeValidator();
		length10Validator.setMin(1);
		length10Validator.setMax(10);
		length10Validator.setErrorMessage(CommonMessages.MaxLength10);
		codeItem = new TextItem("code", "编码");
		codeItem.setDisabled(true);
		nameItem = new TextItem("name", "名称");
		nameItem.setRequired(true);
		nameItem.setValidators(length10Validator);
		nameItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName() != null && event.getKeyName().equals("Enter")) {
					saveBtn.focus();
				}
			}
		});

		form = new DynamicForm();
		form.setSize("300", "80");
		form.setFields(codeItem, nameItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("350", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(15);
		addChild(layout);
	}

	public TextItem getTextItem() {
		return codeItem;
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				Object code = codeItem.getValue();
				Object name = nameItem.getValue();

				Ziyou zy = new Ziyou();
				if (code != null && name != null) {
					zy.setZiyouCode(code.toString());
					zy.setZiyouName(name.toString());
					if (isModify) {
						ZiyouService.Util.getInstance().modifyZiyou(zy, new AsyncCallback<String>() {

							@Override
							public void onSuccess(String result) {
								if (result.equals("")) {
									zc.initData();									
									destroy();
									SC.say(CommonMessages.ModifySuccess);
								} else {
									msg.setContents(result);
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								msg.setContents(caught.getMessage());

							}
						});

					} else {
						ZiyouService.Util.getInstance().createZiyou(zy, new AsyncCallback<String>() {

							@Override
							public void onSuccess(String result) {
								if (result.equals("")) {
									zc.initData();
									msg.setContents(CommonMessages.CreateSuccess);
									codeItem.setValue("*");
									nameItem.setValue("");
									saveBtn.setTitle(CommonMessages.CreateAgain);
								} else {
									msg.setContents(result);
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								msg.setContents(caught.getMessage());

							}
						});
					}
				} else {
					msg.setContents(CommonMessages.CodeNameInputError);

				}
			}
		}

	}
}
